package com.zyz.K_集合.Collections.练习;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author: vick
 * @Date: 2025/3/31
 * @Description:
 * @version: 1.0
 */
public class PokerCard implements Comparable<PokerCard>{
    //花色
    private String suit;
    //点数
    private String rank;

    private static final ArrayList VALID_SUITS = new ArrayList(Arrays.asList("方块","梅花", "红桃","黑桃"));
    private static final ArrayList VALID_RANK = new ArrayList(Arrays.asList("3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K","A", "2", "小王", "大王"));

    public PokerCard(String suit, String rank) {
        if (!isValidSuit(suit) && !isJoker(rank)) {
            throw new RuntimeException("无效花色");
        }
        if (!isValidRank(rank)) {
            throw new RuntimeException("无效点数");
        }
        this.suit = suit;
        this.rank = rank;
    }

    public String getSuit() {
        return suit;
    }

    public String getRank() {
        return rank;
    }

    //是否为大小王
    private boolean isJoker(String rank) {
        return "小王".equals(rank) || "大王".equals(rank);
    }

    //花色是否合法
    private boolean isValidSuit(String suit) {
        return VALID_SUITS.contains(suit);
    }

    //点数是否合法
    private boolean isValidRank(String rank) {
        return VALID_RANK.contains(rank);
    }

    @Override
    public String toString() {
        return suit + rank;
    }

    //定义排序规则
    @Override
    public int compareTo(PokerCard o) {
        //先比较点数
        int thisIndex = VALID_RANK.indexOf(this.rank);
        int otherIndex = VALID_RANK.indexOf(o.rank);
        //如果点数不一样，比较大小
        if (thisIndex != otherIndex) {
            return thisIndex - otherIndex;
        }

        //如果点数相同，比较花色
        int thisSuitsIndex = VALID_SUITS.indexOf(this.suit);
        int otherSuitsIndex = VALID_SUITS.indexOf(this.suit);

        return thisSuitsIndex - otherSuitsIndex;
    }
}
